package com.zcx.qshop.conn;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;
import com.zcx.qshop.entity.Good;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@HttpInlet(Conn.INLET_JSON_ORDER)
public class JsonOrderAsyGet extends QSAsyGet<JsonOrderAsyGet.Info> {

	public String uid, type, page;

	public JsonOrderAsyGet(String uid, String type, String page, AsyCallBack<Info> asyCallBack) {

		super(asyCallBack);

		this.uid = uid;

		this.type = type;

		this.page = page;

 	}

	@Override
	public Info parser(JSONObject object) {

		if(object.optString("message").equals("1")){

			Info info = new Info();

			info.nextpage = object.optString("nextpage");

			info.ispage = object.optString("ispage");

			JSONArray orderArray = object.optJSONArray("orderlist");

			for (int i = 0 ; i < orderArray.length() ; i ++){

				Info.Order order = new Info.Order();

				JSONObject o = orderArray.optJSONObject(i);

				order.id = o.optString("id");

				order.posttime = o.optString("posttime");

				order.type = o.optString("type");

				order.count = o.optString("count");

				order.account = o.optString("account");

				order.ordernum = o.optString("ordernum");

				order.checkinfo = o.optString("checkinfo");

				JSONArray goodArray = o.optJSONArray("goodslist");

				for (int j = 0 ; j < goodArray.length() ; j ++){

					Good good = new Good();

					JSONObject oo = goodArray.optJSONObject(j);

					good.picurl = Conn.SERVICE + oo.optString("picurl");

					good.title = oo.optString("title");

					good.price = oo.optString("price");

					good.unit = oo.optString("unit");

					good.number = oo.optString("number");

					good.one = oo.optString("one");

					good.two = oo.optString("two");

					good.posttime = oo.optString("posttime");

					order.goods.add(good);

				}

				info.orders.add(order);

			}

			return info;

		}

		return null;

	}

	public static class Info{

		public String nextpage, ispage;

		public List<Order> orders = new ArrayList<>();

		public static class Order implements Serializable{

			public String id, posttime, type, count, account, checkinfo, ordernum;

			public List<Good> goods = new ArrayList<>();

		}

	}

}
